import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login.vue'),
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/register.vue'),
    },
    {
      path: '/phone',
      name: 'phone',
      component: () => import('../views/phone.vue'),
    },
    {
      path: '/email',
      name: 'email',
      component: () => import('../views/email.vue'),
    },
  ],
})
const whiteList=['/404','/register','/login','/email','/phone']
router.beforeEach((to,from,next)=>{
  let accessToken=localStorage.getItem('accessToken')
  if(!accessToken){
    if(whiteList.includes(to.path)){
      next()
    }else{
      next('/login')
    }
  }else{
    next()
  }
})
export default router